home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Need help with a STRING that won't go away!!!
- Date: 29 Mar 1996 15:07:59 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jhqgfINN678@anvil.ugrad.cs.ubc.ca>
- References: <4jh7e2$jjr@abel.cc.sunysb.edu> <Pine.A32.3.91.960329131737.148066B-100000@red.weeg.uiowa.edu>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <Pine.A32.3.91.960329131737.148066B-100000@red.weeg.uiowa.edu>,
- The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
- >On 29 Mar 1996, George Hauser wrote:
- >
- >> In this program I am interested in processing unti the EOF, when
- >> I get to it I don't want to loop again to avoid dumping the
- >> data that still is in the string (line).
- >>
- >> As it is, it dumps the exact same record that was on the last line twice!
- >> This causes the import to POST an OCEAN FREIGHT amount 2x!!!
- >
- > You fell in one of the pitfalls awaiting unsuspecting Pascal programmers.
- > Since feof(stream) only tells you that your last read on stream
- >returned EOF, the following loop structure doesn't work the way it does
- >in Pascal:
- >
- > while (!feof(stream1))
- > {
- > fgets(line, stream1);
- > fputs(line, stream2);
- > }
-
- And with good reason. You see, on many operating systems, it's difficult to
- tell whether you are at the end of the file until you actually attempt to
- _read_ past the end of the file. C's stdio logic is probably easier to
- implement because it reflects that.
-
- You try the read, and then if you fail you return EOF.
-
- > What you want is:
- >
- > while (fgets(line, stream1))
- > fputs(line, stream2);
- >
- > since fgets() returns NULL when it hits EOF. This reads "while fgets()
- >reads valid input, fputs() the string read by fgets()."
-
- That's probably not the most appropriate way since fgets is used for line-mode
- input. For a raw copy, you might want to do an unbuffered fread()/fwrite() or a
- getc()/putc() loop. The latter is loop is efficient because it copies data
- directly between one stream's buffer and another (getc/putc are frequently
- implemented as macros, or are inlined by the compiler so doing it a character
- at a time may actually be quite efficient). With fgets() or fread()/fwrite(),
- you are putting the data through an intermediate ``bounce buffer'' rather than
- pumping it directly between two streams.
- --
-
-